// @auth: yan rui
// @Time: 2024-10-31 10:00:07
// @desc: 聊天信息页面，聊天窗口右上角点击“...”进入

import 'package:flutter/material.dart';
import '/components/page/common.dart';
import '/components/list/route_menu_item.dart';
import '/utils/load_image.dart';
import '/utils/setting.dart';

// 封装Switch，样式待优化，后期考虑封装
Widget switchWidget(bool value, Function onChanged) {
  return SizedBox(
    height: 22,
    child: Transform.scale(
      scale: 0.8,
      child: Switch(
        value: value,//当前状态
        onChanged:(value){
          onChanged(value);
        },
        activeColor: const Color(0xFF07c160),
        inactiveThumbColor: DefaultColor.primary,
      ),
    ),
  );
}

// 聊天信息
class ChatInfoPage extends StatelessWidget {
  final String title;
  const ChatInfoPage({
    super.key,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {

    return CommonPage(
      title: title,
      isShowLine: true,
      child: const ChatInfoWidget(),
    );
  }
}

class ChatInfoWidget extends StatefulWidget {
  const ChatInfoWidget({super.key});

  @override
  State<ChatInfoWidget> createState() => _ChatInfoWidgetState();
}

class _ChatInfoWidgetState extends State<ChatInfoWidget> {
  // 免打扰
  bool _switchNoDisturb = true;
  // 置顶
  bool _switchTop = false;
  // 提醒
  bool _switchRemind = true;

  @override
  Widget build(BuildContext context) {

    List listData = [
      {
        'title': '查找聊天记录',
        'onTap': () {
          Navigator.pushNamed(context, 'EmptyPage', arguments: {
            'title': '查找聊天记录',
          });
        },
      },
      {
        'title': '消息免打扰',
        'hasBottom': false,
        'showArrow': false,
        'customRightWidget': switchWidget(_switchNoDisturb, (value) {
          setState(() {
            _switchNoDisturb = value;
          });
        }),
      },
      {
        'title': '置顶聊天',
        'hasBottom': false,
        'showArrow': false,
        'customRightWidget': switchWidget(_switchTop, (value) {
          setState(() {
            _switchTop = value;
          });
        }),
      },
      {
        'title': '提醒',
        'showArrow': false,
        'customRightWidget': switchWidget(_switchRemind, (value) {
          setState(() {
            _switchRemind = value;
          });
        }),
      },
      {
        'title': '设置当前聊天背景',
        'onTap': () {
          Navigator.pushNamed(context, 'EmptyPage', arguments: {
            'title': '设置当前聊天背景',
          });
        },
      },
      {
        'title': '清空聊天记录',
        'onTap': () {
          Navigator.pushNamed(context, 'EmptyPage', arguments: {
            'title': '清空聊天记录',
          });
        },
      },
      {
        'title': '投诉',
        'onTap': () {
          Navigator.pushNamed(context, 'EmptyPage', arguments: {
            'title': '投诉',
          });
        },
      },
    ];

    const double avatarSize = 46;

    Widget topAvatarGroup = Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: Container(
        color: Colors.white,
        child: Row(
          children: [
            Padding(
              padding: const EdgeInsets.all(12.0),
              child: Column(
                children: [
                  loadImageByUrl('assets/images/lufei.png', avatarSize, avatarSize),
                  const Text('前端没钱', style: TextStyle(fontSize: 10, color: Colors.grey),)
                ],
              ),
            )
          ],
        ),
      ),
    );

    return Column(
      children: [
        topAvatarGroup,
        Column(
            children: listData.map((item) => RouteMenuItem(
              leftImgPath: item['leftImgPath'],
              title: item['title'],
              hasBottom: item['hasBottom'] ?? true,
              rightText: item['rightText'],
              showArrow: item['showArrow'] ?? true,
              customRightWidget: item['customRightWidget'],
              onTap: item['onTap'],
            )).toList()
        ),
      ],
    );
  }
}
